package com.zzq.my.jetpacks.jectpackapp.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collections;
import java.util.List;

public class ObjCope {

    /**
     * 2      * 对集合进行深拷贝
     * 3      * 注意需要岁泛型类进行序列化（实现serializable）
     * 4      *
     * 5      * @param src
     * 6      * @param <T>
     * 7      * @return
     * 8      * @throws IOException
     * 9      * @throws ClassNotFoundException
     * 10
     */
    public static <T> List<T> deepCopy(List<T> src) {
        try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
             ObjectOutputStream outputStream = new ObjectOutputStream(byteOut);
        ) {
            outputStream.writeObject(src);
            try (ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
                 ObjectInputStream inputStream = new ObjectInputStream(byteIn);
            ) {
                return (List<T>) inputStream.readObject();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }
}
